package com.newsnetwork.api.interceptors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: HuangDekai
 * @date: 2022/11/7 16:51
 * @version: 1.0
 * @since: jdk8
 * @description: 判断 admin 用户是否登录的拦截器
 */
public class AdminTokenInterceptor extends BaseInterceptor implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(AdminTokenInterceptor.class);

    /**
     * 拦截请求，访问controller之前进行验证, 是否该 admin 已经登录
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String adminUserId = request.getHeader("adminUserId");
        String adminUserToken = request.getHeader("adminUserToken");

        log.info("=====================================================================");
        log.info("AdminTokenInterceptor - adminUserId = {}", adminUserId);
        log.info("AdminTokenInterceptor - adminUserToken = {}", adminUserToken);
        log.info("=====================================================================");

        boolean run = verifyUserIdToken(adminUserId, adminUserToken, REDIS_ADMIN_TOKEN);

        return run;
    }
}
